package com.chx.mapper;

import com.chx.annotation.AutoFill;
import com.chx.dto.CategoryPageQueryDto;
import com.chx.entity.Categories;
import com.chx.enumeration.OperationType;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 */
@Mapper
public interface CategoryMapper {
    @Select("select * from categories")
    List<Categories> getAllCategories();

    @Insert("insert into categories(category_name,parent_id,description,create_time,update_time)" +
            " values" +
            "(#{categoryName},#{parentId},#{description},#{createTime},#{updateTime})")
    @AutoFill(OperationType.INSERT)
    void insert(Categories categories);

    @AutoFill(OperationType.UPDATE)
    void update(Categories categories);

    @Delete("delete from categories where category_id = #{id}")
    void delete(Long id);

    @Select("select * from categories where category_id = #{id}")
    Categories getById(Long id);

    Page<Categories> page(CategoryPageQueryDto categoryPageQueryDto);

    @Select("select count(*) as total from products where category_id = #{id}")
    int getProductById(Long id);

    @Select("select count(*) as total from categories where parent_id = #{id}")
    int getCategoryById(Long id);
}
